package com.hubu.list;

import org.omg.CosNaming.BindingIteratorOperations;

public class SingleList<T> extends AbstractList<T>  implements List<T> {
    protected SingleNode<T> head;

    public SingleList(Comparator<T> comparator, Visitor<T> visitor) {
        super(comparator, visitor);
    }
    @Override
    public boolean add(T value) {
        if(head==null){
            head=createNode(value,null);
            return true;
        }
        SingleNode<T> node=head;
        while(node.next!=null){
            node=node.next;
        }
        node.next=createNode(value,null);
        return true;
    }


    public SingleNode<T> createNode(T value,SingleNode<T> next){
        return new SingleNode<>(value,next);
    }
    @Override
    public void clear() {

    }

    @Override
    public T remove(int index) {
        return null;
    }

    @Override
    public boolean addFirst(T value) {
        return false;
    }

    @Override
    public boolean addLast(T value) {
        return false;
    }


    @Override
    public T set(int index, T value) {
        return null;
    }

    @Override
    public boolean add(int index, T value) {
        return false;
    }
    @Override
    public void remove(T value) {

    }


    public void show(){
        if(head!=null){
            SingleNode<T> node=head;
            while(node!=null){
                System.out.println(node.value);
                node=node.next;
            }
        }

        if(visitor!=null){
            visitor.visitor(head);
        }
    }
    public static class SingleNode<T>{
        public T value;
        public SingleNode<T> next;
        public SingleNode(T value,SingleNode<T> next){
            this.value=value;
            this.next=next;
        }
    }

    public Iterator<T> iterator(){
        Iterator<T> iterator=new SingleListIterator<>(head);
        return iterator;
    }
    static class SingleListIterator<T> implements com.hubu.list.Iterator<T>{
        SingleNode<T> node;
        public SingleListIterator(SingleNode<T> node){
            this.node=node;
        }
        @Override
        public boolean hasNext() {
            return node!=null;
        }

        @Override
        public T next() {
            T value=node.value;
            node=node.next;
            return value;
        }

        @Override
        public T remove() {
            return null;
        }
    }
}
